home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 21 code / Custom GX Printer Drivers / CustomWriter GX 1.0.1 / NewApp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  5.3 KB  |  174 lines  |  [TEXT/MPS ]

  1. /*
  2.     FILENAME
  3.         NewApp.c
  4.  
  5.     DESCRIPTION
  6.         Contains code for all of our message overrides except the old-application
  7.         compatibility, custom I/O, and custom buffering ones.
  8.  
  9.     COPYRIGHT
  10.         Copyright © 1995 Apple Computer, Inc.
  11.         All rights reserved.
  12.     
  13.     Modification history
  14.         05/03/95 - Dave Hersey -    Version 1.0.1 to fix some minor bugs in
  15.                                     CustomBufferingAndIO.c.
  16.  
  17.         01/14/95 - Dave Hersey -    Created from the shell of a hollowed-out
  18.                                     ImageWriter driver.
  19.  
  20.     NOTE: Relevant goodies are listed in MPW's "Mark" menu.
  21.  
  22. */
  23.  
  24. #include "CommonDefines.h"
  25.  
  26.  
  27. /*    -----------------------------------------------------------------------
  28.  
  29.     SD_RasterDataIn is an override of gxRasterDataIn.  It simply takes the
  30.     bitmap data passed to us, and sends it to the "device" using
  31.     Send_GXBufferData.  For this driver, we're being passed 32-bit XRGB
  32.     data (where "X" indicates an unused alpha byte), stripping off the
  33.     unused byte, and sending it to gxBufferData as RGB data.
  34.     
  35.     If we find that we've moved over blank lines, we call WriteBlankLines
  36.     to send RGB bands of white to gxBufferData.  If your device supports
  37.     it, it is far more economical to skip the white space, rather than
  38.     image it.
  39.  
  40.     -----------------------------------------------------------------------    */
  41.  
  42. OSErr SD_RasterDataIn (gxOffscreenHdl hOffscreen, gxRectangle *bandRect, gxRectangle *dirtyRect)
  43. {
  44.     OSErr                anErr;
  45.     char                lockState;
  46.     gxBitmap            *pBitmap;
  47.     unsigned char        *XRGBDataPtr, *RGBDataPtr, *scanStart;
  48.     long                rowNum, colNum, amtToWrite, numBlankLinesToDo;
  49.     Ptr                    baseAddr;
  50.     DriverGlobalsHdl    drvrGlobalsHdl = GetDriverGlobals();
  51.  
  52. #pragma unused(dirtyRect);
  53.  
  54.     // Update the status display to indicate that we're sending data
  55.     // to the "printer."        
  56.  
  57.     anErr = GXReportStatus(kTransmissionStatID, kSendingPartStatIdx);
  58.     require(anErr == noErr, ReportStatus_Failed);
  59.  
  60.  
  61.     // Write some blank lines if we need to. 
  62.  
  63.     numBlankLinesToDo = (bandRect->top >>16) - (*drvrGlobalsHdl)->lastYPos;
  64.     
  65.     if (numBlankLinesToDo > 0)
  66.     {
  67.         anErr = WriteBlankLines(numBlankLinesToDo, bandRect);
  68.         nrequire(anErr, WriteBlankLines_Failed);
  69.     }
  70.  
  71.     // Make sure the offscreen bitmap is locked down, and then repackage the
  72.     // data.  To save space, we're converting the XRGB data to RGB data in
  73.     // the same imaging buffer that was passed to us.
  74.  
  75.     lockState = HGetState((Handle) hOffscreen);
  76.     HLockHi((Handle) hOffscreen);
  77.  
  78.     pBitmap = &(*hOffscreen)->thePlanes[0].theBits;
  79.     baseAddr = RGBDataPtr = XRGBDataPtr = (unsigned char *) pBitmap->image + ((bandRect->left >>16) * 4);
  80.  
  81.  
  82.     // Convert all data in this raster bitmap to 24-bit RGB.  XRGBData
  83.     // points to the next byte of unconverted data, and RGBDataPtr
  84.     // points to the next place to store the converted RGB data.
  85.  
  86.     for (rowNum = (bandRect->top >>16); rowNum < (bandRect->bottom >>16); rowNum++)
  87.     {
  88.         scanStart = XRGBDataPtr;
  89.         
  90.         for (colNum = (bandRect->left >>16); colNum < (bandRect->right >>16); colNum++)
  91.         {
  92.             ++XRGBDataPtr;                // skip those dang alphas!
  93.             *RGBDataPtr = *XRGBDataPtr;    // copy Red component
  94.             ++RGBDataPtr;
  95.             ++XRGBDataPtr;
  96.             *RGBDataPtr = *XRGBDataPtr;    // copy Green component
  97.             ++RGBDataPtr;
  98.             ++XRGBDataPtr;
  99.             *RGBDataPtr = *XRGBDataPtr;    // copy Blue component
  100.             ++RGBDataPtr;
  101.             ++XRGBDataPtr;
  102.         }
  103.             
  104.         if (rowNum == (bandRect->top >>16))    // 1st row?  Store some things for later.
  105.         {
  106.             (*drvrGlobalsHdl)->pixMapRowBytes = (long) XRGBDataPtr - (long) baseAddr;
  107.             (*drvrGlobalsHdl)->pixMapBounds.bottom += (bandRect->bottom  >>16) - (bandRect->top >>16);
  108.             (*drvrGlobalsHdl)->pixMapBounds.right = (bandRect->right >>16) - (bandRect->left >>16);
  109.         }
  110.  
  111.         // Bump XRGBDataPtr to the start of the next scan line.
  112.         XRGBDataPtr = scanStart + (pBitmap->rowBytes & 0x7FFF);
  113.     }
  114.  
  115.     // Write out the data for this band.
  116.  
  117.     amtToWrite = (long) RGBDataPtr - (long) baseAddr;
  118.     anErr = Send_GXBufferData(baseAddr, amtToWrite, gxNoBufferOptions);
  119.  
  120.     // Reset the handle's lock on the way out, and update our
  121.     // lastYPos variable.
  122.  
  123.     HSetState((Handle) hOffscreen, lockState);
  124.  
  125. WriteBlankLines_Failed:
  126. ReportStatus_Failed:
  127.     (*drvrGlobalsHdl)->lastYPos = (bandRect->bottom >>16) +1;
  128.  
  129.     return anErr;
  130. }
  131.  
  132.  
  133. /*    -----------------------------------------------------------------------
  134.  
  135.     WriteBlankLines is a routine used to add blank lines to our output.
  136.     We use this to add "skipped over" lines to our output from
  137.     SD_RasterDataIn.  Since we're printing to a file, we need to add the
  138.     white space as bands of white data.
  139.  
  140.     -----------------------------------------------------------------------    */
  141.  
  142. OSErr WriteBlankLines(long numBlankLinesToDo, gxRectangle *bandRect) 
  143. {
  144.     OSErr                anErr = noErr;
  145.     long                line, aByte, lineLength;
  146.     unsigned char        *blankLine, *whichByte;
  147.  
  148.     // Record some "blank lines" in our file.  NOTE: This assumes we output 24-bit RGB data.
  149.  
  150.     if (numBlankLinesToDo > 0)
  151.     {
  152.         // Create a pointer to hold one white (0xFF, 0xFF, 0xFF…) line.
  153.  
  154.         lineLength = 3 * ((bandRect->right >>16) - (bandRect->left >> 16));
  155.         blankLine = NewPtrSys(lineLength);
  156.         nrequire((anErr = MemError()), NewPtrSys_Failed);
  157.  
  158.         // Make it white.
  159.  
  160.         for (aByte = 0, whichByte = blankLine; aByte < lineLength; aByte++, whichByte++)
  161.             *whichByte = 0xFF;
  162.  
  163.         // Write it to the file as many times as necessary, then dispose of our pointer.
  164.  
  165.         for (line = 0; line < numBlankLinesToDo; line++)
  166.             anErr = Send_GXBufferData((Ptr) blankLine, lineLength, gxNoBufferOptions);
  167.  
  168.         DisposePtr(blankLine);
  169.     }
  170.  
  171. NewPtrSys_Failed:
  172.     return anErr; 
  173. }
  174.